import allure
import pytest
from common.assert_util import AssertUtil
from common.request_util import RequestUtil
from core.yaml_case_parser import yaml_case_parser, case_refresh
from utils.allure_dynamic import dynamic_title
from common.logger_util import LoggerUtil
from utils.yaml_util import read_extract_yaml

# class TestRunner:
assert_util = AssertUtil()
res = {}

@allure.step("用例执行")
@pytest.mark.parametrize("caseinfo", yaml_case_parser("testcases"))
def test_case_execute(caseinfo):

    dynamic_title(caseinfo)

    re_util = RequestUtil()
    logger = LoggerUtil().get_logger()

    caseinfo = case_refresh(caseinfo, read_extract_yaml())
    print(caseinfo)

    logger.info("--------接口用例开始执行---------")
    for key,value in caseinfo.items():
        match key:
            case "request":
                for r_key,r_value in value.items():
                    logger.info("接口数据%s: %s" % (r_key, r_value))
                res = re_util.send_request(**value)
                logger.info("接口响应数据：%s" %res.json())
            case "validate":
                for va in value:
                    assert_util.res_assert(res, va)
                logger.info("接口断言成功")
            case "extract":
                re_util.data_extract(res, value)
            case _:
                continue
    logger.info("--------接口用例执行结束---------")
